package com.hl.lib_miniui.view.texts;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.hl.base_module.util.screen.DensityUtil;
import com.hl.lib_miniui.R;

/**
 * 一行两列文本
*@Author: hl
*@Date: created at 2020/6/23 14:45
*@Description: com.hl.lib_miniui.view.texts
*/
public class OneRowTwoColumn extends LinearLayout {
    private AppCompatTextView textViewL, textViewR;

    public OneRowTwoColumn(Context context) {
        super(context);
        init(context, null);
    }

    public OneRowTwoColumn(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public OneRowTwoColumn(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public OneRowTwoColumn(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    /**
     * 初始化
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {
        // 水平布局
        setOrientation(HORIZONTAL);

        // 从自定义属性获取值
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ColumnRow_Value);
        // 获取关键字
        String left_texttip = ta.getString(R.styleable.ColumnRow_Value_left_texttip);
        String right_texttip = ta.getString(R.styleable.ColumnRow_Value_right_texttip);
        // 获取关键字颜色
        int left_textcolor = ta.getColor(R.styleable.ColumnRow_Value_left_textcolor, 0x999999);
        int right_textcolor = ta.getColor(R.styleable.ColumnRow_Value_right_textcolor, 0x333333);
        // 获取关键字大小属性的设置
        int left_textsize = (int) ta.getDimension(R.styleable.ColumnRow_Value_left_textsize, 28);
        int right_textsize = (int) ta.getDimension(R.styleable.ColumnRow_Value_right_textsize, 28);
        ta.recycle();

        LinearLayout.LayoutParams layoutParamsL = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textViewL = new AppCompatTextView(context);
        textViewL.setLayoutParams(layoutParamsL);
        textViewL.setTextSize(TypedValue.COMPLEX_UNIT_PX, left_textsize);
        textViewL.setTextColor(left_textcolor);
        textViewL.setText(TextUtils.isEmpty(left_texttip) ? "标题" : left_texttip);

        LinearLayout.LayoutParams layoutParamsR = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParamsR.leftMargin = DensityUtil.dip2px(context, 20);
        textViewR = new AppCompatTextView(context);
        textViewR.setLayoutParams(layoutParamsR);
        textViewR.setTextSize(TypedValue.COMPLEX_UNIT_PX, right_textsize);
        textViewR.setTextColor(right_textcolor);
        textViewR.setText(TextUtils.isEmpty(right_texttip) ? "----" : right_texttip);

        addView(textViewL);
        addView(textViewR);
    }
}
